package org.quickjsf.core.base;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.quickjsf.common.Constants.BOOLEAN_OP;
import org.quickjsf.common.util.DomainUtil;
import org.quickjsf.core.annotations.ColumnModelHandler;
import org.quickjsf.core.annotations.MRelationHandler;
import org.quickjsf.core.annotations.MappingRelation;
import org.quickjsf.core.base.viewmodel.vo.ColumnModel;
import org.quickjsf.core.base.viewmodel.vo.FieldFilter.CondType;
import org.quickjsf.core.base.viewmodel.vo.FieldFilter.CondTypeName;
import org.quickjsf.core.datamodel.OptionType;

public class QuickJsfContainer implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 6395882642806647527L;
	
	private String domainBasePackage = "org.quickjsf.example.domain";

	private Map<String, List<ColumnModel>> columnModelMap = new HashMap<String, List<ColumnModel>>();
	private Map<String, List<MappingRelation>> relationMap = new HashMap<String, List<MappingRelation>>();
	
	private Map<String, Map<String, List<OptionType>>> optionTypeMap = new HashMap<String, Map<String, List<OptionType>>>();
	
	private LinkedHashMap<String, Byte> condTypeMap = null;
	private LinkedHashMap<String, Boolean> booleanOptionMap = null;
	
	//默认为中文
	public static Locale locale = Locale.SIMPLIFIED_CHINESE;
	
	public QuickJsfContainer() throws Exception {
		// 防止Tomcat 7中Integer类型的id自动设置为'0'
		System.setProperty("org.apache.el.parser.COERCE_TO_ZERO", "false");
		
		initCondTypeMap();
		initBooleanOptionMap();
		
		Class<? extends Object>[] domainClasses = DomainUtil.getPackageClasses(domainBasePackage);
		ColumnModelHandler.handlerColumnModel(domainClasses, columnModelMap, optionTypeMap);
		
		for (List<ColumnModel> columnModels : columnModelMap.values()) {
			for (ColumnModel columnModel : columnModels) {
				System.out.println(columnModel.getHeader()+columnModel.getOrder());
			}
		}
		
		MRelationHandler.handlerRelation(domainClasses, relationMap);
	}
	
	public void initCondTypeMap() {
		condTypeMap = new LinkedHashMap<String, Byte>();
		condTypeMap.put(CondTypeName.EQ, CondType.EQ);
		condTypeMap.put(CondTypeName.LE, CondType.LE);
		condTypeMap.put(CondTypeName.GT, CondType.GT);
		condTypeMap.put(CondTypeName.BETWEEN, CondType.BETWEEN);
		condTypeMap.put(CondTypeName.LIKE, CondType.LIKE);
	}
	
	public void initBooleanOptionMap() {
		booleanOptionMap = new LinkedHashMap<String, Boolean>();
		booleanOptionMap.put(BOOLEAN_OP.TRUE, Boolean.TRUE);
		booleanOptionMap.put(BOOLEAN_OP.FALSE, Boolean.FALSE);
	}
	
	public LinkedHashMap<String, Byte> getCondTypeMap() {
		return condTypeMap;
	}

	public LinkedHashMap<String, Boolean> getBooleanOptionMap() {
		return booleanOptionMap;
	}

	public Map<String, List<ColumnModel>> getColumnModelMap() {
		return columnModelMap;
	}

	public Map<String, Map<String, List<OptionType>>> getOptionTypeMap() {
		return optionTypeMap;
	}

	public Map<String, List<MappingRelation>> getRelationMap() {
		return relationMap;
	}
}
